﻿using System;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace Core.Graphics
{
    [Serializable()]
    [XmlRoot("AutoTileInfo")]
    public class AutoTileInfo : IXmlSerializable
    {
        public string Name { get; set; }
        public Tile Plain { get; set; }

        public Tile Up { get; set; }
        public Tile Down { get; set; }
        public Tile Right { get; set; }
        public Tile Left { get; set; }

        public Tile UpRight { get; set; }
        public Tile UpLeft { get; set; }
        public Tile DownRight { get; set; }
        public Tile DownLeft { get; set; }

        public Tile CornerUpRight { get; set; }
        public Tile CornerUpLeft { get; set; }
        public Tile CornerDownRight { get; set; }
        public Tile CornerDownLeft { get; set; }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            Name = reader.GetAttribute("Name");
            Plain = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("")));

            Up = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("Up")));
            Down = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("Down")));
            Right = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("Right")));
            Left = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("Left")));

            UpRight = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("UpRight")));
            UpLeft = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("UpLeft")));
            DownRight = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("DownRight")));
            DownLeft = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("DownLeft")));

            CornerUpRight = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("CornerUpRight")));
            CornerUpLeft = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("CornerUpLeft")));
            CornerDownRight = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("CornerDownRight")));
            CornerDownLeft = Tile.GetTileFromBitVector(Convert.ToInt32(reader.GetAttribute("CornerDownLeft")));
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Name", Name);
            writer.WriteAttributeString("Plain", Tile.GetTileAsBitVector(Plain).ToString());

            writer.WriteAttributeString("Up", Tile.GetTileAsBitVector(Up).ToString());
            writer.WriteAttributeString("Down", Tile.GetTileAsBitVector(Down).ToString());
            writer.WriteAttributeString("Right", Tile.GetTileAsBitVector(Right).ToString());
            writer.WriteAttributeString("Left", Tile.GetTileAsBitVector(Left).ToString());

            writer.WriteAttributeString("UpRight", Tile.GetTileAsBitVector(UpRight).ToString());
            writer.WriteAttributeString("UpLeft", Tile.GetTileAsBitVector(UpLeft).ToString());
            writer.WriteAttributeString("DownRight", Tile.GetTileAsBitVector(DownRight).ToString());
            writer.WriteAttributeString("DownLeft", Tile.GetTileAsBitVector(DownLeft).ToString());

            writer.WriteAttributeString("CornerUpRight", Tile.GetTileAsBitVector(CornerUpRight).ToString());
            writer.WriteAttributeString("CornerUpLeft", Tile.GetTileAsBitVector(CornerUpLeft).ToString());
            writer.WriteAttributeString("CornerDownRight", Tile.GetTileAsBitVector(CornerDownRight).ToString());
            writer.WriteAttributeString("CornerDownLeft", Tile.GetTileAsBitVector(CornerDownLeft).ToString());
        }

        #endregion
    }

    [Serializable()]
    [XmlRoot("AutoTileCollection")]
    public class AutoTileInfoCollection : Collection<AutoTileInfo>
    {
        public static AutoTileInfoCollection GlobalAutoTileSets
        {
            get 
            {
                if (_GlobalAutoTileSets == null)
                    _GlobalAutoTileSets = new AutoTileInfoCollection();

                return _GlobalAutoTileSets;
            }
            set
            {
                _GlobalAutoTileSets = value; 
            }
        }

        protected static AutoTileInfoCollection _GlobalAutoTileSets;

        public AutoTileInfo GetByName(string Name)
        {
            foreach (AutoTileInfo ati in this)
            {
                if (ati.Name == Name)
                    return ati;
            }

            return null;
        }
    }
}
